﻿using System;
using System.Collections.Generic;
using System.Text;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.DAL;
using RFIDStockManagement.Common.Public.Enums;
namespace RFIDStockManagement.BLL
{
    public class OutStockDataBuilder
    {
        private OutStockBill _value;

        public OutStockDataBuilder()
        {
            _value = new OutStockBill();
            
        }

        public OutStockDataBuilder(OutStockBill value)
        {
            _value = value;
           
        }


        #region 属性
        /// <summary>
        /// 制单人
        /// </summary>
        public int BillerID
        {
            get
            {
                return _value.BillerID;
            }
            set
            {
                _value.BillerID = value;
            }
        }

        /// <summary>
        /// 仓库ID
        /// </summary>
        public long StockID
        {
            get
            {
                return _value.FStockID;
            }
            set
            {
                _value.FStockID = value;
            }
        }
        /// <summary>
        /// 入库人
        /// </summary>
        public string OutStockPerson
        {
            get
            {
                return _value.FOutStockPerson;
            }
            set
            {
                _value.FOutStockPerson = value;
            }
        }

        ///// <summary>
        ///// 供应商ID
        ///// </summary>
        //public long SupplierId
        //{
        //    get
        //    {
        //        return _value.FSupplierID;
        //    }
        //    set
        //    {
        //        _value.FSupplierID = value;
        //    }
        //}

        /// <summary>
        /// 用途
        /// </summary>
        public string Note
        {
            get
            {
                return _value.FNote;
            }
            set
            {
                _value.FNote = value;
            }
        }

        #endregion

        #region 公有函数
        public void SetData(OutStockBill value)
        {
            _value = value;

        }

        /// <summary>
        /// 添加一条明细,如果不存在当前itemid的不会被添加进去
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="rfid"></param>
        public void AddDetailInfo(long itemId, string rfid)
        {
            InStockBillEntry curEntry = null;
            InStockDetail newDetail;
            foreach (InStockBillEntry x in _value.Entries)
            {
                if (x.FItemID == itemId)
                {
                    curEntry = x;
                    break;
                }
            }

            if (curEntry != null)
            {
               
                newDetail = new InStockDetail();
                newDetail.FRFID = rfid;
                newDetail.FItemID = itemId;
                curEntry.RFIDDetails.Add(newDetail);
            }

        }

        /// <summary>
        /// 添加入库单分录和明细信息
        /// </summary>
        /// <param name="itemid"></param>
        /// <param name="stockplaceid"></param>
        /// <param name="unitid"></param>
        /// <param name="rfid"></param>
        public void AddDetailInfo(long itemid, long stockplaceid, long unitid, string rfid)
        {
            InStockBillEntry curEntry = null;
            InStockDetail newDetail;

            foreach (InStockBillEntry x in _value.Entries)
            {
                if (x.FItemID == itemid)
                {
                    curEntry = x;
                    break;
                }
            }

            //添加当前分录信息进去
            if (curEntry == null)
            {
                curEntry = new InStockBillEntry();
                curEntry.FItemID = itemid;
                curEntry.FStockPlaceID = stockplaceid;
                curEntry.FUnitID = unitid;
                curEntry.FScanTime = DateTime.Now;
                _value.Entries.Add(curEntry);
            }

            //添加当前rfid明细进去
            newDetail = new InStockDetail();
            newDetail.FRFID = rfid;
            newDetail.FItemID = itemid;
            curEntry.RFIDDetails.Add(newDetail);

        }

        /// <summary>
        /// 设置当前rfid中出库了多少数量
        /// </summary>
        /// <param name="rfid">当前rfid</param>
        /// <param name="itemId">当前物料</param>
        /// <param name="qty">要设置的数量</param>
        /// <returns></returns>
        public bool SetQty(string rfid, int itemId, double qty)
        {
            bool retVal = true;
            foreach (InStockBillEntry x in _value.Entries)
            {
                if (x.FItemID == itemId)
                {
                    foreach (InStockDetail xx in x.RFIDDetails)
                    {
                        if (xx.FRFID.Equals(rfid))
                        {
                            xx.FQty = qty;
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 添加一条入库单分录信息
        /// </summary>
        /// <param name="itemid"></param>
        /// <param name="stockplaceid"></param>
        /// <param name="unitid"></param>
        public void AddEntryInfo(long itemid, int stockplaceid,long unitid)
        {
            InStockBillEntry curEntry = null;
            foreach (InStockBillEntry x in _value.Entries)
            {
                if (x.FItemID == itemid)
                {
                    curEntry = x;
                    break;
                }
            }

            if (curEntry == null)
            {
                curEntry = new InStockBillEntry();
                curEntry.FItemID = itemid;
                curEntry.FStockPlaceID = stockplaceid;
                curEntry.FUnitID = unitid;
                curEntry.FScanTime = DateTime.Now;
                _value.Entries.Add(curEntry);
            }

        }

        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <returns></returns>
        public OutStockBill GetData()
        {
            foreach (InStockBillEntry x in _value.Entries)
            {
                x.FQty = x.RFIDDetails.Count;
                
            }

            return _value;
        }

        #endregion
        
    }
}
